// pages/user/afterSale/afterSale.js
var app = getApp();
var _PAGE = 1;
var _PAGESIZE = 10;
var flowAftersale = app.globalData._network_path + 'flowAftersale.png';
var empty='';
var load= true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 1,
    applyDetail: false,
    states: ['已提交', '已审核', '已接单', '已服务', '已完成'],
    Img:'../../../images/tick.png',
    unImg:'../../../images/spot-1.png',
    applyDataDetail:[],//订单列表
    recordData:[],
    hasMore: false, //数据是否加载完成
    loading: false,
    imgArr: [],//图片数组
    isType:false,
    load_show:false,
    flowAftersale: flowAftersale

  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('flowAftersale', flowAftersale);
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    _PAGE = 1;
    var index = wx.getStorageSync('afterIndex')||1;
    that.setData({
      index: index
    })
    if(index ==1){
      that.getapplyDataDetail();
    }else{
      that.getWorkList();      
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    _PAGE =1;

  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var hasMore = that.data.hasMore;
    if (!hasMore) {
      return false;
    }
    _PAGE ++;
    that.setData({
      loading: true
    })
    var index = that.data.index;
    if(index==1){
      that.getapplyDataDetail();
    } else if (index == 2) {
      that.getWorkList();
    }
  },

  /**
   * 切换
   */
  bindChange: function (e) {
    var index = e.currentTarget.dataset.type;
    this.setData({
      index: index,
      recordData:[],
      applyDataDetail:[],
      applyDetail: false,
      loading:false,
      hasMore: false,
      load_show: false,
    })
    load= true;
    wx.setStorageSync('afterIndex', index);
    _PAGE = 1;
    if (index == 2) {
      this.getWorkList();
    } else {
      this.getapplyDataDetail();
    }
  },
  // 售后申请合同详情
  goApplyDetail:function(e){
    wx.navigateTo({
      url: '../econtract/econtract?contract_id=' + e.currentTarget.dataset.contract_id,
    })
  },
  // 售后记录详情
  goDetail:function(e){
    //console.log(e.currentTarget.dataset.detail);
    wx.navigateTo({
      url: '../afterSaleDetail/afterSaleDetail?detail=' + e.currentTarget.dataset.detail,
    })
  },
  // 评价
  evaluate:function(e){
    //console.log(e.currentTarget.dataset.work_order_id);
    wx.navigateTo({
      url: '../evaluate/evaluate?work_order_id=' + e.currentTarget.dataset.work_order_id,
    })
  },
  //申请售后
  applyAfterSale: function (e) {
    //console.log();
    var device_no = e.currentTarget.dataset.device_no;
    var equipment_id = e.currentTarget.dataset.equipment_id;
    wx.navigateTo({
      url: '../applyAfterSale/applyAfterSale?device_no=' + device_no + '&equipment_id=' + equipment_id,
    })
  },
  //获取订单列表
  getapplyDataDetail(){
    var that = this;
    
    app.ajax({
      url: 'User/Equipmentlists/getRentalLists',
      method: "POST",
      msg: '加载中...',
      load: load,
      data: {
        page: _PAGE,
        pagesize: _PAGESIZE
      },
      success: function (res) {
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              applyDataDetail: res.data.data,
            });
            empty= false;
          } else {
            that.setData({
              applyDataDetail: that.data.applyDataDetail.concat(res.data.data)
            });
            empty= false;
          }
          // 是否加载更多
          var hasMore = true;
          if (res.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
          load= false
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false
            })
            empty= true;
          } else {
            that.setData({
              hasMore: false,
              loading: false
            })
            empty= false;
          }
        }
        that.setData({
          load_show: true
        })
      } 
    })
  },
  //获取售后记录列表
  getWorkList() {
    var that = this;   
    app.ajax({
      url: 'User/Work/getWorkList',
      method: "POST",
      load: load,
      msg: '加载中...',
      data: {
        page: _PAGE,
        pagesize: _PAGESIZE
      },
      success: function (res) {
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              recordData: res.data.data,
            });
            empty=false;
          } else {
            that.setData({
              recordData: that.data.recordData.concat(res.data.data)
            });
            empty= false;
          }
          // 是否加载更多
          var hasMore = true;
          if (res.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
          load= false;
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false
            })
            empty= true
          } else {
            that.setData({
              hasMore: false,
              loading: false
            })
            empty= false;
          }
        }
        that.setData({
          load_show: true,

        })
      }
    })
  },
  // 跳转设备详情
  goEquipmentDetail(e){
    var that = this;
    var equipmentId = e.currentTarget.dataset.equipmentid;
    var status = e.currentTarget.dataset.status;
    if(status){
      return;
    }
    wx.navigateTo({
      url: '../../facilityDetail/facilityDetail?equipment_id=' + equipmentId,
    })

  },
  //图片报错  ../../../images/icon_sxin.png
  avatar_error(e) {
    var that = this, recordData = that.data.recordData, inds = e.currentTarget.dataset.index;
    recordData[inds].main_pic = '../../../images/none.png'
    that.setData({
      recordData: recordData
    })
  },
  //催单
  reminder(e){
    var that = this, work_order_id=e.currentTarget.dataset.work_order_id;
    app.ajax({
      url: 'User/Work/workReminder',
      method: "POST",
      data: { work_order_id: work_order_id, reminder_content:''},
      success: function (res) {
        if (res.data.code = 1000) {
          app.showToast(res.data.message, "none", 2000, function () { });
        } else {
          app.showToast(res.data.message, "none", 2000, function () { });
        }
      }

    })  
  },
  // 跳转付款记录
  goPayRecord: function(){
    wx.navigateTo({
      url: '../payRecord/payRecord',
    })
  },
  // 查看流程大图
  viewPicture(e) {
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },
})